#![cfg_attr(debug_assertions, allow(dead_code, unused_imports, unused_variables, non_camel_case_types))]
#[macro_use] extern crate rocket;

mod protocol;
mod services;
mod routes;
mod tasks;
mod workflows;
mod error;
mod log;
mod prelude;
mod abstractions;
mod builtins;
mod datafile;
mod test_direct;
use std::process::exit;

use rocket::{fs::FileServer, launch};
use rocket_dyn_templates::Template;
use services::{manager as service_manager, routes as service_routes};
use tasks::{routes as task_routes, manager as task_manager};
use workflows::{manager as workflow_manager, routes as workflow_routes};
use builtins::stimulus_extraction_impl::test_extract_stimulus_frames;
#[launch]
pub fn rocket() -> _ {
    if std::env::args().any(|arg| arg == "--testexsf") {
        test_extract_stimulus_frames();
        exit(0);
    }
    
    let _ = protocol::datatypes::DataType::new().unwrap();
    task_manager::init_task_manager().unwrap();
    workflow_manager::init_workflow_manager().unwrap();
    service_manager::init_service_manager().unwrap();

    if std::env::args().any(|arg| arg == "--testdirect") {
        test_direct::test_direct();
        exit(0);
    }

    rocket::build()
        .mount("/", routes![routes::index, routes::start_guide, routes::dashboard, routes::running,
                            routes::services, routes::workflow, routes::test])
        .mount("/doc", routes![routes::calib, routes::service, routes::types, routes::user_defined_service, routes::future])
        .mount("/_", routes![
            service_routes::list_services, 
            service_routes::get_service, 
            service_routes::add_service, 
            service_routes::delete_service, 
            service_routes::refresh_service, 
            service_routes::service_start_task,
            task_routes::get_tasks,
            task_routes::get_task_progress,
            task_routes::terminate_task,
            task_routes::get_task_result,
            task_routes::get_task,
            task_routes::create_task,
            workflow_routes::valid_tasks,
            workflow_routes::add_workflow,
            workflow_routes::start_workflow,
            workflow_routes::get_all_workflows,
            workflow_routes::get_workflow,
            workflow_routes::delete_workflow,
            workflow_routes::get_running_workflows,
            workflow_routes::delete_running_workflow,
            workflow_routes::get_running_workflow_result,
        ])
        .mount("/doc", FileServer::from("doc"))
        .mount("/static", FileServer::from("static"))
        .attach(Template::fairing())
        .attach(service_manager::ServiceManagerFairing)
}
